<?php
class mdl_position extends CI_Model{ 
	
	var $tabel = 'tm_master_position';
	
	function __construct(){
		parent::__construct();
	} 
 
	function data($p=array()){
	
		$where = '';
		
		if( isset($p['column']) && isset($p['keyword']) ){
			if($p['column'] != '' || $p['keyword'] != ''){
				if($p['column'] == 'all'){
					$where = " AND ( position_code like '%".pTxt($p['keyword'])."%'
									 OR position_desc like '%".pTxt($p['keyword'])."%'
									 OR dept_name like '%".pTxt($p['keyword'])."%')";
				}else{				
					$where = " AND ".$p['column']." like '%".pTxt($p['keyword'])."%' ";
				}
			}
		}
		$sql = "
		 	select 
				SQL_CALC_FOUND_ROWS a.*,b.dept_name
			FROM ".$this->tabel." a,tm_master_dept b
			WHERE a.position_dept_id = b.dept_id
				".$where."
			ORDER BY a.position_id DESC 
		";	
		
		if( isset($p['limit']) && isset($p['offset']) ){
			$offset = empty($p['offset'])?0:$p['offset'];				
			$sql .= " LIMIT ".$offset.",".$p['limit']." ";
		}
		$query = $this->db->query($sql);
		
		$found_rows = $this->db->query('SELECT FOUND_ROWS() as found_rows');

		return array(
			"data" 	=> $query->result(),
			"count"	=> $found_rows->row()
		);
	}	
	
}